<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="行程标题" prop="title">
				<el-input v-model="form.title" placeholder="行程标题" clearable maxlength="80"></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="行程类型" prop="newsType">
						<sc-select v-model="form.ledActType" :params="{code:'ledActType'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="指定领导" prop="leader">
						<select-user-dialog v-model:accountId="form.leader" :mode="false"/>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="开始时间" prop="beginTime" maxlength="80">
						<el-date-picker :disabled-date="publishDateAfter" v-model="form.beginTime" placeholder="开始时间" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="结束时间" prop="endTime">
						<el-date-picker :disabled-date="publishDateAfter" v-model="form.endTime" placeholder="结束时间" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="人员权限" prop="userRole">
				<select-user-dialog v-model:accountId="form.userRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="部门权限">
				<select-dept-dialog v-model:deptId="form.deptRole" :mode="true"/>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="24">
					<el-form-item label="职务权限">
						<select-user-level-dialog v-model:levelId="form.levelRole" :mode="true"/>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="行程内容" >
				<el-input type="textarea" rows="4" v-model="form.remark" placeholder="行程内容" clearable maxlength="380"></el-input>
			</el-form-item>
			<msg-box-check-box v-model:msgType="form.msgType" code="leader-activity"></msg-box-check-box>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">{{ btName }}</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import selectDeptDialog from "@/views/module/selectDeptDialog";
import selectUserLevelDialog from "@/views/module/selectUserLevelDialog";
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";

export default {
	emits: ['success', 'closed'],
	components: {
		selectUserLevelDialog,
		msgBoxCheckBox,
		selectUserDialog,
		selectDeptDialog,
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			isShowMsg:false,
			mode: "add",
			btName: "保存",
			titleMap: {
				add: '发布行程',
				edit: '编辑行程',
			},
			form: {
				recordId:'',
				title: '',
				ledActType: '',
				leader: '',
				beginTime:'',
				endTime:'',
				userRole: '',
				deptRole: '',
				levelRole: '',
				remark:'',
				msgType:''
			},
			options: [{
				value: '0',
				label: '请选择'
			}],
			rules: {
				title: [
					{required: true, message: '行程标题不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			if (this.mode == "add") {
				this.btName = "发布";
			} else {
				this.btName = "保存";
			}
			return this
		},
		publishDateAfter(time){
			return time.getTime()<(Date.now()-24*60*60*1000)
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.info.leadActivity.insertLeadActivity.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.info.leadActivity.updateLeadActivity.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.recordId = data.recordId
			this.form.title = data.title
			this.form.ledActType = data.ledActType
			this.form.leader = data.leader
			this.form.userRole = data.userRole
			this.form.deptRole = data.deptRole
			this.form.levelRole = data.levelRole
			this.form.beginTime = data.beginTime
			this.form.endTime = data.endTime
			this.form.remark = data.remark
			this.form.msgType = data.msgType
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
